<template>
  <div
    :class="{'show':show}"
    class="header-search"
  >
    <el-popover
      placement="top-start"
      title=""
      width="200"
      trigger="click"
    >
      <el-select
        ref="headerSearchSelect"
        v-model="search"
        :remote-method="querySearch"
        filterable
        default-first-option
        remote
        clearable
        :placeholder="siteSearch"
        class="header-search-select"
        @change="change"
      >
        <el-option
          v-for="item in options"
          :key="item.path"
          :value="item"
          :label="item.title.join(' > ')"
        />
      </el-select>
      <!--      <i slot="reference" class="el-icon-search"></i>-->
      <el-button
        slot="reference"
        type="text"
        class="search-button"
      >
        <i class="el-icon-search search-icon pointer" />
      </el-button>
    </el-popover>
  </div>
</template>

<script>
// fuse is a lightweight fuzzy-search module
// make search results more in line with expectations
import Fuse from 'fuse.js';
import Vue from 'vue';
import path from 'path';
import {Button, Popover, Select} from "element-ui";

Vue.use(Popover);
Vue.use(Select);
Vue.use(Button);
/**
 * 头部搜索组件
 * @author maolin
 */
export default {
  name: 'HeaderSearch',
  data () {
    return {
      search: '',
      options: [],
      searchPool: [],
      show: false,
      fuse: undefined,
      siteSearch: this.$t('navbar.search.siteSearch')
    };
  },
  computed: {
    routes () {
      return this.$store.getters.permission_routes;
    }
  },
  watch: {
    routes () {
      this.searchPool = this.generateRoutes(this.routes);
    },
    searchPool (list) {
      this.initFuse(list);
    },
    show (value) {
      if (value) {
        document.body.addEventListener('click', this.close);
      } else {
        document.body.removeEventListener('click', this.close);
      }
    }
  },
  mounted () {
    this.searchPool = this.generateRoutes(this.routes);
  },
  methods: {
    /**
     * 点击搜索按钮,展开搜索框并聚焦
     * @public
     */
    click () {
      this.show = !this.show;
      if (this.show) {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus();
      }
    },
    /**
     * 关闭搜索框并取消聚焦
     * @public
     */
    close () {
      this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur();
      this.options = [];
      this.show = false;
    },
    /**
     * 改变路由,
     * @public
     */
    change (val) {
      this.$router.push(val.path);
      this.search = '';
      this.options = [];
      this.$nextTick(() => {
        this.show = false;
      });
    },
    initFuse (list) {
      this.fuse = new Fuse(list, {
        shouldSort: true,
        threshold: 0.4,
        location: 0,
        distance: 100,
        maxPatternLength: 32,
        minMatchCharLength: 1,
        keys: [{
          name: 'title',
          weight: 0.7
        }, {
          name: 'path',
          weight: 0.3
        }]
      });
    },
    // Filter out the routes that can be displayed in the sidebar
    // And generate the internationalized title
    /**
     * 生成路由
     * @private
     */
    generateRoutes (routes, basePath = '/', prefixTitle = []) {
      let res = [];

      for (const router of routes) {
        // skip hidden router
        if (router.hidden) { continue; }

        const data = {
          path: path.resolve(basePath, router.path),
          title: [...prefixTitle]
        };

        if (router.meta && router.meta.title) {
          data.title = [...data.title, router.meta.title];

          if (router.redirect !== 'noRedirect') {
            // only push the routes with title
            // special case: need to exclude parent router without redirect
            res.push(data);
          }
        }

        // recursive child routes
        if (router.children) {
          const tempRoutes = this.generateRoutes(router.children, data.path, data.title);
          if (tempRoutes.length >= 1) {
            res = [...res, ...tempRoutes];
          }
        }
      }
      return res;
    },
    /**
     *
     * @private
     */
    querySearch (query) {
      if (query !== '') {
        this.options = this.fuse.search(query);
      } else {
        this.options = [];
      }
    }
  }
};
</script>

<style lang="scss" scoped>
  .header-search {
    position: relative;
    font-size: 0 !important;
    .search-button{
      padding: 0;
      .search-icon {
        width: 18px;
        height: 60px;
        line-height: 60px;
        font-size: 18px;
        color: #fff;
      }
    }


    .header-search-select {
      z-index: 99;
      font-size: 18px;
      transition: width 0.2s;
      width: 0;
      overflow: hidden;
      background: #fff;
      border-radius: 0;
      display: inline-block;
      vertical-align: middle;

      ::v-deep .el-input__inner {
        border-radius: 0;
        border: 0;
        padding-left: 0;
        padding-right: 0;
        box-shadow: none !important;
        border-bottom: 1px solid #d9d9d9;
        vertical-align: middle;
      }
    }

    &.show {
      .header-search-select {
        width: 210px;
        background-color: #fff;
        margin-left: 10px;
      }
    }
  }
</style>
